
<%@page import="com.ddd.entity.News"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.NewsService"%>
<%@page import="com.ddd.context.Parameters"%>
<%@page import="com.ddd.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    String message = request.getParameter("message");
    String gotoPage = request.getParameter("gotoPage");

    Parameters para = new Parameters(request);
    if (Util.isNotBlank(gotoPage)) {
        para.setCurrentPage(Integer.parseInt(gotoPage));
    } else {
        para.setCurrentPage(1);
    }

    // load data
    NewsService newsService = new NewsService(request);
    ArrayList newsList = newsService.loadAll();

%>
<%  
    ArrayList newsList_s = new ArrayList();
    //搜尋功能
    for (int i=0;i<newsList.size();i++) {
        News temp1=(News) newsList.get(i); 
        
        String temp2=temp1.getId();

        if(request.getParameter("search_value")!=""&&request.getParameter("search_value")!=null){
            if(temp2.contains(request.getParameter("search_value"))){
                newsList_s.add(newsList.get(i));
            }
        }else{ 
            newsList_s.add(newsList.get(i));
        }     
    }
    para.setTotalSize(newsList_s.size());
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            function doSelect(){
               $('#form2').submit();
            }
            
            function doAdd(){
                var news = $('#news_new').val();
                
                $('#mode').val("add");
                $('#news').val(news);
               
                $( "#dialog_add" ).dialog({
                    title: "新增最新消息",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "新增": function() {
                            $( this ).dialog( "close" );
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });  
            }
       
            function doUpdate (i){
                var tmpName = '#news_' + i;
                var news = $(tmpName).val();
                
                tmpName = '#cid_' + i;
                var cid = $(tmpName).val();
                
                tmpName = '#ctime_' + i;
                var ctime = $(tmpName).val();
                
                tmpName = '#uid_' + i;
                var uid = $(tmpName).val();
                
                tmpName = '#utime_' + i;
                var utime = $(tmpName).val();
                
                $('#mode').val("update");
                $('#id').val(i);
                $('#news').val(news);
                $('#cid').val(cid);
                $('#ctime').val(ctime);
                $('#uid').val(uid);
                $('#utime').val(utime);
               
                
                $( "#dialog_update" ).dialog({
                    title: "修改最新消息",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "修改": function() {
                            $( this ).dialog( "close" );
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });  
            }
            
            function doDelete (id){
                $( "#dialog_delete" ).dialog({
                    title: "刪除最新消息",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doNews?mode=delete&id=" + id;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            function gotoPage(page){
                window.location = "news.jsp?gotoPage=" + page;
            }
            
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <div>
                            <form id="form2" name="form2" action="/DDD/BE/news.jsp" method="post">
                                搜尋：
                                <input type="text" name="search_value" id="search_value"/>
                                <input type="button" value="搜尋" name="search_submit" id="search_submit" onclick="doSelect()"/>
                            </form>
                        </div>
                        <!-- starrt #content -->
                        <div id="content">
                            <table>
                                <thead>
                                    <tr>
                                        <th>序號</th>
                                        <th>最新消息</th>
                                        <th>建立者</th>
                                        <th>建立時間</th>
                                        <th>修改者</th>
                                        <th>修改時間</th>
                                        <th>刪除</th>
                                        <th>修改</th>
                                    </tr>
                                </thead>
                                <%
                                    int left = para.getPageSize();
                                    for (int i = (para.getCurrentPage() - 1) * para.getPageSize(); i < para.getCurrentPage() * para.getPageSize() && i < para.getTotalSize(); i++) {
                                        left--;
                                        News n = (News) newsList_s.get(i);
                                %>
                                <tr>
                                    <td><%=n.getId()%></input></td>
                                    <td><textarea cols="35" rows="10" id="news_<%=n.getId()%>" name="news_<%=n.getId()%>"><%=n.getNews()%></textarea></td>
                                    <td><input type="text" id="cid_<%=n.getId()%>" name="cid_<%=n.getId()%>" value="<%=n.getCid()%>" readonly size="10"></input></td>
                                    <td><input type="text" id="ctime_<%=n.getId()%>" name="ctime_<%=n.getId()%>" value="<%=n.getCtime()%>" readonly size="14"></input></td>
                                    <td><input type="text" id="uid_<%=n.getId()%>" name="uid_<%=n.getId()%>" value="<%=n.getUid()%>" readonly size="10"></input></td>
                                    <td><input type="text" id="ctime_<%=n.getId()%>" name="utime_<%=n.getId()%>" value="<%=n.getUtime()%>" readonly size="14"></input></td>
                                    <td><a href='javascript:doUpdate("<%=n.getId()%>");'>修改</a></td>
                                    <td><a href='javascript:doDelete("<%=n.getId()%>");'>刪除</a></td>
                                </tr>
                                <%}%>

                                <!-- 新增 -->
                                <tr>
                                    <td>&nbsp;</td>
                                    <td><textarea cols="35" rows="10" id="news_new" name="news_new"></textarea></td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td><a href='javascript:doAdd();'>新增</a></td>
                                </tr>

                                <%for (int i = 0; i < left; i++) {%>
                                <tr>
                                    <%for (int ii = 0 ; ii<8 ; ii++ ){%>
                                        <td>&nbsp;</td>
                                    <%}%>
                                </tr>
                                <%}%>
                            </table>
                            <br /> 
                            <p align="center">
                                <%=para.getCurrentPage()%> / <%=para.getTotalPage()%> (總筆數: <%=para.getTotalSize()%>)
                            </p>
                            <p align="center">
                                <%if (para.getCurrentPage() > 1) {%>
                                <input type="button" value="上一頁" onclick='gotoPage(<%=para.getCurrentPage() - 1%>)'>
                                <%}%>
                                <%if (para.getCurrentPage() < para.getTotalPage()) {%>
                                <input type="button" value="下一頁" onclick='gotoPage(<%=para.getCurrentPage() + 1%>)'>
                                <%}%>
                            </p>

                        </div>
                        <!-- end #content -->
                        <form id="form1" name="form1" action="../doNews" method="post">
                            <input type="hidden" id="mode" name="mode"></input>
                            <input type="hidden" id="id" name="id"></input>
                            <input type="hidden" id="news" name="news"></input>
                            <input type="hidden" id="cid" name="cid"></input>
                            <input type="hidden" id="ctime" name="ctime"></input>
                            <input type="hidden" id="uid" name="uid"></input>
                            <input type="hidden" id="utime" name="utime"></input>
                        </form>
                        <!-- end #sidebar -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_add" style="display: none">
            <p>是否要新增最新消息？</p>
        </div>

        <div id="dialog_update" style="display: none">
            <p>是否要修改最新消息？</p>
        </div>

        <div id="dialog_delete" style="display: none">
            <p>是否要刪除最新消息？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>
